const app = getApp();
const api = app.apiInstance
import {FLOWTYPEARRAY,FLOWSTATUSARRAY} from "../../public/utils/const.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    subStatusList: [
      {
        value: 'Complete',
        label: '完成'
      },
      {
        value: 'NotYetStarted',
        label: '未开始'
      },
      {
        value: 'Pass',
        label: '通过'
      },
      {
        value: 'Reject',
        label: '拒绝'
      },
    ],
  },

  tabChange(e){
  },

  async init({id}){
    const that = this;

    const staffList = await api.get(`/v1/openApi/employee/search/staffList`);
    this.setData({
     staffList:staffList.rows
   })

    if(id){
      const formData = await api.get(`/v1/openApi/resource/change/details/${this.data.id}`);

      formData.data.value.forEach(item=>{
        item.auditorName = this.data.staffList.find(subitem => subitem.sapId ==
        item.auditor)?.staffName;
        item.statusName = this.data.subStatusList.find(subitem => subitem.value ==
        item.status)?.label;

      })
       this.setData({
        dataList:this.combineList(formData.data.value)
     })

    }
    
  },
  gotodetail(e){
   
    const item = e.currentTarget.dataset.item;

    if(item.remark.indexOf('确认')>-1){
      wx.navigateTo({
        url: '/subPages/bianGengQueRen/bianGengQueRen?isEdit=false&sapid='+item.auditor+'&id='+item.changeId,
      })
    }

    if(item.remark.indexOf('审核')>-1){
      wx.navigateTo({
        url: '/subPages/bianGengShenHe/bianGengShenHe?isEdit=false&sapid='+item.auditor+'&id='+item.changeId,
      })
    }
    if(item.remark.indexOf('验收')>-1){
      wx.navigateTo({
        url: '/subPages/bianGengYanShou/bianGengYanShou?isEdit=false&sapid='+item.auditor+'&id='+item.changeId,
      })
    }


    // url: '/subPages/bianGengShenHe/bianGengShenHe?id='+e.currentTarget.dataset.item.id,
    // url: '/subPages/bianGengQueRen/bianGengQueRen?id='+e.currentTarget.dataset.item.id,
    // url: '/subPages/bianGengYanShou/bianGengYanShou?id='+e.currentTarget.dataset.item.id,
  },

  combineList(list){
    if(list&&list.length==0){
      return
    }

    const tempArray = [];
    list.forEach(item=>{
      const tempObj = tempArray.find(subItem => item.step==subItem.step);
      if(tempObj){
        tempObj.auditorName+="或"+item.auditorName
      }
      else{
        tempArray.push(item)

      }

    })
    return tempArray

  },


  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id
    })
    this.init(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})